"use client"; import { Wallet } from "@/api/user"; import { ChannelType, getWithDrawApi, WithDrawType } from "@/api/withdraw"; import { clearWallet } from "@/app/[locale]/(navbar)/withdraw/actions"; import Box from "@/components/Box"; import ButtonOwn from "@/components/ButtonOwn"; import Empty from "@/components/Empty"; import TipsModal, { ModalProps } from "@/components/TipsModal"; import { useUserInfoStore } from "@/stores/useUserInfoStore"; import { useWalletStore } from "@/stores/useWalletStore"; import { isEmail } from "@/utils"; import { ActionSheet, Form, Input, Toast } from "antd-mobile"; import type { Action } from "antd-mobile/es/components/action-sheet"; import { FormInstance } from "antd-mobile/es/components/form"; import { useTranslations } from "next-intl"; import Link from "next/link"; import { FC, Fragment, useRef, useState } from "react"; import "./page.scss"; interface Props { channels: WithDrawType[]; wallet: Wallet; } export enum ChannelEnum { CPF = 1, Email, Phone, CNPJ, } type FieldValueType = { account_no: string; channel_id: (typeof ChannelEnum)[keyof typeof ChannelEnum]; type: number; }; interface MobileFieldProps { value?: FieldValueType; onChange?: (value: FieldValueType) => void; actions: Array; } const MobileField: FC = (props) => { const { actions, value = { account_no: "", channel_id: actions[0].id, type: actions[0].type }, onChange, } = props; let [visible, setVisible] = useState(false); const t = useTranslations("WithdrawPage"); let [prefix, setPrefix] = useState(actions[0]); let [placeholder, setPlaceholder] = useState("000.000.000-00"); const onAction = (item: any) => { setPrefix(item); onRealValueChange(""); if (item.type === ChannelEnum.CPF) { setPlaceholder("000.000.000-00"); return; } if (item.type === ChannelEnum.Phone) { setPlaceholder("11 dígitos"); return; } if (item.type === ChannelEnum.Email) { setPlaceholder("Email"); return; } }; const onRealValueChange = (value: string) => { if (onChange) { onChange({ account_no: value, channel_id: prefix.id, type: prefix.type }); } }; return ( <>
setVisible(true)} className={"mr-[0.1rem] flex flex-shrink-0"}> {prefix.text}
setVisible(false)} /> ); }; const WithdrawWidget: FC = (props) => { const t = useTranslations(); const { channels, wallet } = props; const score = useWalletStore((state) => state.score)!; const withdrawRef = useRef(null); const formRef = useRef(null); const [activeWallet, setActiveWallet] = useState(channels[0]); const walletAction = activeWallet && activeWallet.channels?.map((item) => ({ text: ChannelEnum[item.type], key: item.id, ...item, })); const defaultActions: Array = [ { text: "CPF", key: "CPF", id: ChannelEnum.CPF }, { text: t("WithdrawPage.Número"), key: "Celular", id: ChannelEnum.Phone }, { text: "EMAIL", key: "EMAIL", id: ChannelEnum.Email }, ]; const userInfo = useUserInfoStore((state) => state.userInfo); const initParams = { channel: "", amount: "", passport: userInfo.passport, user_name: userInfo.user_name, }; const AmountValidator = (rules: any, value: string) => { const num = +value; if (num > activeWallet.max_amount) { return Promise.reject( new Error(t("WithdrawPage.amountReg", { max: activeWallet.max_amount })) ); } if (score && num > score) { return Promise.reject(new Error(t("WithdrawPage.amountMaxReg"))); } return Promise.resolve(); }; const ChannelValidator = (rules: any, value: FieldValueType) => { if (!value.account_no) return Promise.reject(new Error(t("WithdrawPage.channel"))); if (value.type === ChannelEnum.CPF) { return value.account_no.length !== 11 ? Promise.reject(new Error(t("WithdrawPage.cpfReg"))) : Promise.resolve(); } if (value.type === ChannelEnum.Email) { return isEmail(value.account_no) ? Promise.resolve() : Promise.reject(new Error(t("WithdrawPage.EmailReg"))); } if (value.type === ChannelEnum.Phone) { return value.account_no.length < 11 ? Promise.reject(new Error(t("WithdrawPage.phoneReg"))) : Promise.resolve(); } return Promise.resolve(); }; const onFinish = async (value: any) => { const params = { ...value, ...value.channel, amount: +value.amount }; getWithDrawApi(params) .then((res) => { if (res.code === 200) { Toast.show(t("code.200")); } }) .catch((error) => { Toast.show(t(`code.${error.data.code}`)); }); await clearWallet(); }; if (!activeWallet) return ; return ( <>
{channels.map((item) => { return (

{ formRef.current?.resetFields(); setActiveWallet(item); }} > {item.name}

); })}

{t("WithdrawPage.Certifique")}

{t("WithdrawPage.keyTips")}

{/* form */}
{t("WithdrawPage.Saque")}} >

{t("WithdrawPage.Tipo")}

{activeWallet.channels?.length && ( )}

{t("WithdrawPage.Vincule")}

{t("WithdrawPage.Montante")} (BRL):

  • {t("WithdrawPage.SaqueDisponivel")}{" "} {wallet.score} BRL
  • {t("WithdrawPage.Valor")} 0 BRL{" "} withdrawRef.current?.onOpen()} >
  • {t("WithdrawPage.Para")},{" "} {t("WithdrawPage.Aposte")}
  • 1
  • 2
  • 3
  • 4
); }; export default WithdrawWidget;